package cn.buk.qms.web;

import cn.buk.tms.hotel.dto.HotelCity;
import cn.buk.tms.hotel.dto.HotelSearchResult;
import cn.buk.tms.hotel.dto.RoomPriceDto;
import cn.buk.tms.hotel.sc.HotelSearchCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yfdai
 */
@RestController
@RequestMapping("/hotel")
public class HotelController extends BaseController {

    private final cn.buk.tms.hotel.service.HotelService hotelService;

    @Autowired
    public HotelController(cn.buk.tms.hotel.service.HotelService hotelService) {
        this.hotelService = hotelService;
    }

    /**
     * 酒店城市
     * @param request
     * @return
     */
    @RequestMapping(value = "/cities")
    public List<HotelCity> searchHotelCities(HttpServletRequest request) {
        List<HotelCity> cities = hotelService.searchHotelCities();

        cities = cities.stream().sorted(Comparator.comparing(HotelCity::getCityEnglishName)).collect(Collectors.toList());

        return cities;
    }

    @RequestMapping(value = "/avail")
    public HotelSearchResult searchHotel(HttpServletRequest request,
                                         @ModelAttribute("sc") HotelSearchCriteria sc) {
        if (sc.getCheckInDate() == null || sc.getCheckOutDate() == null) {
            return null;
        }

        return hotelService.searchHotel(sc);
    }

    /**
     * 根据售卖房型id，查找价格
     * @param request
     * @return
     */
    @RequestMapping(value = "/price")
    public List<RoomPriceDto> searchRoomPrices(HttpServletRequest request,
                                               @RequestParam("roomId") int roomId,
                                               @RequestParam("checkInDate") Date checkInDate,
                                               @RequestParam("checkOutDate") Date checkOutDate) {
        return hotelService.searchRoomPrice(roomId, checkInDate, checkOutDate);
    }
}
